package com.platform.wechat.entity;


import com.platform.wechat.common.Sha1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author CX
 * @create 2021-07-08 17:01
 */
public class Wechat114Msg {

    private String signature;//微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数

    private String timestamp;//时间戳

    private String nonce;//随机数

    private String echostr;//随机字符串

    public boolean checkSignature(){
        List<String> list = new ArrayList<>();
        list.add(this.timestamp);
        list.add(this.nonce);
        list.add("20210708");
        Collections.sort(list);
        String str = "";
        for (int i = 0;i < list.size();i++){
            str += list.get(i);
        }
        String sha1 = Sha1.encode(str);
        if(sha1.equals(this.signature)){
            return true;
        }
        return false;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getNonce() {
        return nonce;
    }

    public void setNonce(String nonce) {
        this.nonce = nonce;
    }

    public String getEchostr() {
        return echostr;
    }

    public void setEchostr(String echostr) {
        this.echostr = echostr;
    }

    public Wechat114Msg(String signature, String timestamp, String nonce, String echostr) {
        this.signature = signature;
        this.timestamp = timestamp;
        this.nonce = nonce;
        this.echostr = echostr;
    }

    public Wechat114Msg() {
    }

    @Override
    public String toString() {
        return "WechatMsg{" +
                "signature='" + signature + '\'' +
                ", timestamp='" + timestamp + '\'' +
                ", nonce='" + nonce + '\'' +
                ", echostr='" + echostr + '\'' +
                '}';
    }
}
